<?php
/**
 * Created by Daniel X -- PhpStorm.  2020/7/16 15:16
 */
namespace App\Http\Controllers\Traits;

trait TransFormer
{
    /**
     * 设置返回数据格式
     * @param $code
     * @param $msg
     * @param array $data
     * @return array
     */
    public function transFormer($code, $msg, $data = array())
    {
        return [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ];
    }


    public function transFormerE($code, $msg, $data = array())
    {
        $result = [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ];
        exit(json_encode($result));
    }
    /**
     * 错误提示
     * @param $code
     * @param $msg
     * @param array $data
     * @return array
     */
    public function transFormerError($code, $msg)
    {
        return [
            'code' => $code,
            'msg' => $msg
        ];
    }

    /**
     * 正确返回
     * @param $code
     * @param $msg
     * @param array $data
     * @return array
     */
    public function transFormerOk( $msg, $data = [])
    {
        return [
            'code' => 200,
            'msg' => $msg,
            'data' => $data
        ];
    }

    // 登录 专用
    public function transFormerUser($code, $msg, $token ,$data = array())
    {
        return [
            'code' => $code,
            'msg' => $msg,
            'token' => $token,
            'data' => $data,
        ];
    }

    /**
     * @param $page
     * @return float|int
     * 使用方法：->offset($this->execute($request->page))->limit(10)
     */
    public static function execute($page, $limit = 10)
    {
        return ($page - 1) * $limit;
    }
}